package org.chen.processor;

import com.alibaba.nacos.common.utils.StringUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.chen.model.ImageInfo;
import org.chen.model.Section;
import org.chen.service.CrawlerService;
import org.springframework.beans.factory.annotation.Autowired;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public abstract class AbstractESPNPageProcessor implements PageProcessor {

    private final AtomicInteger pageCounter;

    @Setter
    private boolean crawlImages = true;  // 默认爬取图片

    public AbstractESPNPageProcessor() {
        this.pageCounter = new AtomicInteger(0); // 确保每个实例有独立的计数器
    }

    @Override
    public void process(Page page) {

        // 处理页面内容
        processPage(page);

        // 验证页面有效性
        if (!isValidPage(page)) {
            log.info("Skipping invalid page: {}", page.getUrl());
            page.setSkip(true);
            return;
        }

        // 增加计数并检查是否需要停止
        int newCount = pageCounter.incrementAndGet();
        log.info("Processed {} pages out of {}", newCount,
                this.getClass().getSimpleName());
    }

    // 新增：验证页面内容是否有效
    protected boolean isValidPage(Page page) {
        // 检查标题
        String title = page.getResultItems().get("title");
        if (StringUtils.isEmpty(title)) {
            log.debug("Page skipped: Empty title");
            return false;
        }

        // 检查作者
        String author = page.getResultItems().get("author");
        if (StringUtils.isEmpty(author)) {
            log.debug("Page skipped: Empty author");
            return false;
        }

        // 检查导语和章节
        List<String> leadParagraphs = page.getResultItems().get("leadParagraphs");
        List<Section> sections = page.getResultItems().get("sections");

        boolean hasContent = (leadParagraphs != null && !leadParagraphs.isEmpty()) ||
                (sections != null && !sections.isEmpty());

        if (!hasContent) {
            log.debug("Page skipped: No content found");
            return false;
        }

        // 如果需要爬取图片，检查图片
        if (crawlImages) {
            List<ImageInfo> images = page.getResultItems().get("images");
            if (images == null || images.isEmpty()) {
                log.debug("Page skipped: No images found when crawlImages is true");
                return false;
            }
        }

        return true;
    }

    // 子类必须实现的抽象方法
    protected abstract void processPage(Page page);

    public int getProcessedCount() {
        return pageCounter.get();
    }

}

